﻿using System;

namespace AspnetUtil.Util
{
    /// <summary>
    /// 对象工具类
    /// </summary>
    public static class BeanUtil
    {
        /// <summary>
        /// 转换对象的类型
        /// </summary>
        /// <param name="tIn"></param>
        /// <typeparam name="TIn">输入类型</typeparam>
        /// <typeparam name="TOut">输出类型</typeparam>
        /// <returns></returns>
        public static TOut Transfer<TIn, TOut>(TIn tIn)
        {
            TOut tOut = Activator.CreateInstance<TOut>();
            var tInType = tIn.GetType();
            foreach (var itemOut in tOut.GetType().GetProperties())
            {
                var itemIn = tInType.GetProperty(itemOut.Name);
                ;
                if (itemIn != null)
                {
                    itemOut.SetValue(tOut, itemIn.GetValue(tIn));
                }
            }

            return tOut;
        }
    }
}